<template>
  <div class="page">
    <div class="c-main p20">
      <div class="c-body">
        <el-row type="flex" justify="center">
          <el-col :span="14">
            <el-form ref="form" :model="pageAjax.data" label-width="120px">
              <el-form-item label="平台名称：">
                <el-input
                  placeholder="请输入平台名称"
                  v-model="pageAjax.data.name"
                ></el-input>
              </el-form-item>
              <el-form-item label="请选择上级平台">
                <el-cascader
                  v-model="pageAjax.data.parentId"
                  :props="orgCascaderCp.config.props"
                  :options="orgSupAjax.data"
                >
                </el-cascader>
              </el-form-item>
              <el-form-item label="平台类型：">
                <el-select
                  v-model="pageAjax.data.mask"
                  placeholder="请选择平台类型"
                >
                  <el-option
                    v-for="item in $store.state.GLOBAL.PLATFORM_TYPE.list"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="rSubmitForm">提交</el-button>
                <el-button @click="rBack">取消</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>
<script>
import { addPlatform, getPlatformDetail } from "@/api/authority.js";
import { getOffspringnTree } from "@/assets/js/util.js";
import _ from "lodash";

export default {
  name: "system_authorityEditPlatform",
  data: () => ({
    pageAjax: {
      btnDisabled: false,
      btnLoading: false,
      loading: false,
      data: {
        name: "", // 平台名称
        corpId: 1, // 所属公司
        parentId: 0, // 上级平台
        parentIds: 0,
        mask: 1, // 平台类型
        level: 0 // 级别
      }
    },
    orgSupAjax: {
      loading: false,
      data: [],
      orginData: []
    },

    orgCascaderCp: {
      config: {
        props: {
          value: "id",
          label: "name",
          checkStrictly: true
        }
      }
    }
  }),
  created() {
    let _that = this;

    _that.getOrgData();
    _that.getPageData();
  },
  methods: {
    rSubmitForm() {
      let _that = this;
      let formData = _.cloneDeep(_that.pageAjax.data);
      let orginData = _that.orgSupAjax.orginData;

      if (!formData.name) {
        _that.$message.error("平台名称不能为空");
        return;
      }
      let parentId = formData.parentId
        ? formData.parentId[formData.parentId.length - 1]
        : formData.parentId;

      for (var i = orginData.length; i--; ) {
        if (orginData[i].id == parentId) {
          formData.level = orginData[i].level;
          break;
        }
      }

      if (_that.pageAjax.loading) {
        return;
      }

      _that.pageAjax.loading = true;
      addPlatform({
        id: formData.id,
        name: formData.name,
        mask: formData.mask,
        parentId: parentId,
        level: formData.level
      })
        .then(resp => {
          let respData = resp.data || {};
          if (respData.code === _that.STATUS.success[0]) {
            _that.$store
              .dispatch("getPlatformList", { latest: true })
              .then(() => {
                _that.$message({
                  type: "success",
                  message: "添加成功",
                  onClose: () => {
                    _that.goRefresh(-1);
                  },
                  duration: 1200
                });
              });
          } else {
            _that.$message.error(respData.msg || "添加失败");
          }
        })
        .finally(() => {
          _that.pageAjax.loading = false;
        });
    },
    getPageData() {
      let _that = this;

      getPlatformDetail({ params: { id: _that.$route.query.id } })
        .then(resp => {
          let respData = resp.data || {};

          if (respData.code === _that.STATUS.success[0]) {
            _that.pageAjax.data = respData.data;
          } else {
            _that.$message.error(respData.msg || "获取失败");
          }
        })
        .finally(() => {});
    },
    getOrgData() {
      let _that = this;
      let arr = _.cloneDeep(_that.$store.state.allPlatformList) || [];
      _that.orgSupAjax.orginData = _.cloneDeep(arr);
      // 去掉全部
      for (var i = arr.length; i--; ) {
        if (arr[i].id == 0) {
          arr.splice(i, 1);
          break;
        }
      }

      _that.orgSupAjax.data = getOffspringnTree(_.cloneDeep(arr), 0);
    },
    rBack() {
      this.goRefresh(-1);
    }
  }
};
</script>
<style lang="scss" scoped>
.page {
  .s-search {
    border-bottom: 1px solid #eee;
  }
  .c-main {
    margin-top: 20px;
  }
  .c-pagination {
    margin-top: 20px;
    text-align: center;
  }
}
</style>
